import {postBanner} from '@/base/api/BannersApi';
import {deepAssign} from '@/base/util/Objects';
import {getAgentList} from '@/base/api/AgentApi';
import {Page} from '@/base/Page';
import {getCompanyList} from '@/base/api/CompanyApi';
import {Company} from '@/base/model/Company';
import {Banners} from '@/base/model/Banners';
import {DeviceSort} from '@/base/model/DeviceSort';
import {DeviceModel, DeviceModelName} from '@/base/model/DeviceModel';
import {store} from "@/store/Store";
import {Agent} from "@/base/model/Agent";

export class FormBloc {
  model: any = {
    companyId: 0,
    agentId: 1,
    bannername: '',
    imageurl:'',
    status: 0,
    // linkurl:'',
    position:0,
  };
  loading = false;
  fileList: Array<any> = []
  num: number = 0;
  sortList: any = [];
  companyList: any = []
  // sortId: number = 0;
  // modelId: number = 0;
  isDetail = false;
  type: string | undefined;
  company: Page<Company> = new Page<Company>([], 0, 1, 10, 0, []);
  angent: Page<Agent> = new Page<Agent>([], 0, 1, 15, 0, []);

  getAllSort(pageIndex: number, pageSize: number, sortDirection: string) {
    this.loading = true;
    let that = this;
    return getAgentList({
      pageIndex,
      pageSize,
      sortDirection,
    }).then((res) => {
      this.sortList = res.data.items.map(function (item) {
        var obj = {id: 0, agentName: ''};
        // @ts-ignore
        obj.id = item.id;
        obj.agentName = item.agentName;
        return obj;
      });
      return this.sortList;
    }).finally(() => {
      this.loading = false;
    });
  }

  getCompanyList(pageIndex: number,
                 pageSize: number,
                 sortDirection: string) {
    return getCompanyList({pageIndex, pageSize, sortDirection, name: '', createBegin: ''})
      .then((res) => {
        deepAssign(this.company, res.data)
        this.companyList = res.data.items.map(item => {
          let obj = {companyId: 0, companyName: ''}
          obj.companyId = item.id;
          obj.companyName = item.name
          return obj
        })
        return res;
      }).finally(() => {
        this.loading = false;
      });
  }



  addModel(model) {
    this.loading = true;
    return postBanner({
      sort:1,
      agentId: model.agentId,
      bannername: model.bannername,
      companyId: model.companyId,
      imageurl: model.imageurl,
      status:model.status,
      position: model.position,
    }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

  // reset() {
  //   console.log(store.state.user.account);
  //   location.reload();
  // }

}
